<?php
    function procedure_call($pr)
    {  
        require("./Conn.php");
        $array = Array();
        if (!$conn) 
        { 
            $m = oci_error();
            trigger_error(htmlentities($m['message']), E_USER_ERROR);
        } 
        $curs = oci_new_cursor($conn);
        $stid = oci_parse($conn, "begin ".$pr."(:cursorAux); end;");
        oci_bind_by_name($stid, ":cursorAux", $curs, -1, OCI_B_CURSOR);
        oci_execute($stid);
        oci_execute($curs); 
        while ($row = oci_fetch_array($curs, OCI_ASSOC)) 
        { 
            array_push($array, $row);
        } 
        return $array;
    }   
    $tipo = filter_input(INPUT_POST, 'tipo_'); 
    $path_ = filter_input(INPUT_POST, 'path_'); 
    echo $tipo."me la suda";
    
    if($tipo != "Parcial")
    {
        $ctrls = procedure_call("CONTROLFILES");  
        $logs = procedure_call("LOGS");  
        $archives = procedure_call("ARCHIVELOGSS");  
        $spfile = procedure_call("SPFILE");    
        $tblspcs = procedure_call("DATAFILES");  
    } 
    else 
    {
        $ctrls = json_decode(filter_input(INPUT_POST, 'controls_')); 
        $logs = json_decode(filter_input(INPUT_POST, 'logs_')); 
        $archives = json_decode(filter_input(INPUT_POST, 'archives_')); 
        $spfile = json_decode(filter_input(INPUT_POST, 'spfile_')); 
        $tblspcs = json_decode(filter_input(INPUT_POST, 'tbs_'));  
        $datas = json_decode(filter_input(INPUT_POST, 'tbs2_'));  
    } 
    $arc_mode = procedure_call("ARCMODE");
    $dolar = "$";
    $arc_mode_ = implode($arc_mode[0]);  
    $sRun = "set head off\nset feed off\nset line 200\nset serveroutput on\n";
    if($arc_mode_ == "NOARCHIVELOG") 
        $sRun.= "SHUTDOWN IMMEDIATE;\nSTARTUP MOUNT;\nALTER DATABASE ARCHIVELOG;\nALTER DATABASE OPEN;\n".
            "ALTER SYSTEM ARCHIVE LOG START;\nALTER SYSTEM SWITCH LOGFILE;"; 
    $inc_mode = procedure_call("INCMODE"); 
    $inc_mode_ = implode($inc_mode[0]);   
//    if($tipo == "Incremental" && $inc_mode != "ENABLED")
//        $sRun.= "ALTER DATABASE ENABLE BLOCK CHANGE TRACKING;\n"; 
//    if($tipo != "Incremental" && $inc_mode == "ENABLED")
//        $sRun.= "ALTER DATABASE DISABLE BLOCK CHANGE TRACKING;\n"; 
    $i = 0;
    foreach ($tblspcs as $t)
    {
        if($tipo != "Parcial")
        {
            $t_ = $t["TABLESPACE_NAME"];
            $d = $t["FILE_NAME"];
        } 
        else 
        {
            $t_ = $t;
            $d = $datas[$i++];
        }
        $sRun.="ALTER TABLESPACE ".$t_." BEGIN BACKUP;\n"; 
        $sRun.="HOST XCOPY ".$d." ".$path_." \n"; 
        $sRun.="ALTER TABLESPACE ".$t_." END BACKUP;\nALTER SYSTEM SWITCH LOGFILE;\n";
    }  
    if($tipo != "Parcial")
    {
        foreach($ctrls as $c)
            $sRun.=$dolar."xcopy ".implode($c)." ".$path_." \n"; 
        foreach($logs as $l)
            $sRun.=$dolar."xcopy ".implode($l)." ".$path_." \n"; 
        foreach($archives as $a)
            $sRun.=$dolar."xcopy ".implode($a)." ".$path_." \n"; 
        foreach($spfile as $s)
            $sRun.=$dolar."xcopy ".implode($s)." ".$path_." \n"; 
    }
    else
    {
        foreach($ctrls as $c)
        {
            if(strpos($c,'\\\\') == FALSE)
                $sRun .= $dolar."xcopy ".$c." ".$path_." \n"; 
            else 
                $sRun .= $dolar."xcopy ".json_decode ($c)." ".$path_." \n"; 
        }
        foreach($logs as $l)
        { 
            if(strpos($l,'\\\\') == FALSE)
                $sRun .= $dolar."xcopy ".$l." ".$path_." \n"; 
            else 
                $sRun .= $dolar."xcopy ".json_decode ($l)." ".$path_." \n"; 
        }
        foreach($archives as $a)
        { 
            if(strpos($a,'\\\\') == FALSE)
                $sRun .= $dolar."xcopy ".$a." ".$path_." \n"; 
            else 
                $sRun .= $dolar."xcopy ".json_decode ($a)." ".$path_." \n"; 
        }
        foreach($spfile as $s)
        { 
            if(strpos($s,'\\\\') == FALSE)
                $sRun.=$dolar."xcopy ".$s." ".$path_." \n"; 
            else 
                $sRun.=$dolar."xcopy ".json_decode ($s)." ".$path_." \n"; 
        }
    }
    $Run_ = fopen("run_orcl.sql", "w+") or die("no abrió run.sql");  
    fwrite($Run_, $sRun); 
    fclose($Run_);